﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonsterPorcessor : MonoBehaviour
{

    [SerializeField]
    private Monster m_monsterModel;

    private Sprite[] m_monsterTextures;

    public static MonsterPorcessor instance { get { return m_instance; } }
    private static MonsterPorcessor m_instance = null;
    private void Awake()
    {
        if (m_monsterTextures == null)
            m_monsterTextures = Resources.LoadAll<Sprite>("Images/Monsters");
        m_monsterModel.gameObject.SetActive(false);
        Debug.Log(m_monsterTextures.Length);
        m_instance = this;
    }

    public Monster CreateMonster(string pUserId, int pSpriteInex)
    {
        var monster_clone = Instantiate(m_monsterModel.gameObject, this.transform, false).GetComponent<Monster>();
        if (pSpriteInex > m_monsterTextures.Length - 1)
            pSpriteInex = m_monsterTextures.Length - 1;
        monster_clone.SetSprite(m_monsterTextures[pSpriteInex], pSpriteInex);
        monster_clone.SetUserId(pUserId);
        monster_clone.name = pUserId;
        monster_clone.gameObject.SetActive(true);
        return monster_clone;
    }


    //public Monster[] SplitMin(Monster pMonster)
    //{
    //    var tg = new List<Monster>();
    //    var totalScore = pMonster.score / 2;
    //    var minSplitScore = 2 * SCORE_PER_UNIT;
    //    if (totalScore < minSplitScore)
    //    {
    //        var childMonster = Instantiate(m_monsterModel.gameObject, this.transform, false).GetComponent<Monster>();
    //        childMonster.SetSprite(m_monsterTextures[pMonster.spriteIndex], pMonster.spriteIndex);
    //        childMonster.SetUserId(pMonster.userId);
    //        childMonster.name = pMonster.userId + "-ThornSplit";
    //        childMonster.SetScore(totalScore);
    //        childMonster.gameObject.SetActive(true);
    //        tg.Add(childMonster);
    //    }

    //    while (totalScore > SCORE_PER_UNIT)
    //    {
    //        var childMonster = Instantiate(m_monsterModel.gameObject, this.transform, false).GetComponent<Monster>();
    //        childMonster.SetSprite(m_monsterTextures[pMonster.spriteIndex], pMonster.spriteIndex);
    //        childMonster.SetUserId(pMonster.userId);
    //        childMonster.name = pMonster.userId + "-ThornSplit";
    //        totalScore -= SCORE_PER_UNIT;
    //        tg.Insert(0, childMonster);
    //        Debug.Log("----");
    //    }

    //    return tg.ToArray();
    //}

    //public Monster[] Bisection(Monster pMonster)
    //{
    //    var tg = new List<Monster>();
    //    var totalScore = pMonster.score;
    //    var splitCount = 2;
    //    for (int index = 0; index < splitCount; index++)
    //    {
    //        var childMonster = Instantiate(m_monsterModel.gameObject, this.transform, false).GetComponent<Monster>();
    //        childMonster.SetSprite(m_monsterTextures[pMonster.spriteIndex], pMonster.spriteIndex);
    //        childMonster.SetScore(pMonster.score / splitCount);
    //        childMonster.SetUserId(pMonster.userId);
    //        childMonster.name = pMonster.userId + "-UserSplit";
    //        childMonster.transform.position = pMonster.transform.position;
    //        childMonster.gameObject.SetActive(true);
    //        tg.Add(childMonster);
    //    }
    //    return tg.ToArray();
    //}
}
